package com.example.project.util;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by 阿炜啊 on 2020/7/19.
 */

public class ThreadManager {

    private static volatile ThreadManager sManager = null;
    private final ThreadPoolExecutor mExecutor;

    private ThreadManager() {
        //ThreadPoolExecutor
        /**
         * (int corePoolSize,核心线程的数量，3
         * int maximumPoolSize,最大线程数量，10（3核心线程，7临时工）
         * long keepAliveTime,非核心线程没有任务的情况下，存活的时间
         * TimeUnit unit,时间单位
         * BlockingQueue<Runnable> workQueue: 任务排队策略
         */
        mExecutor = new ThreadPoolExecutor(3,
                10,
                1,
                TimeUnit.MINUTES,
                new LinkedBlockingQueue<Runnable>());
    }

    public static ThreadManager getInstance() {
        if (sManager == null) {
            synchronized (ThreadManager.class) {
                if (sManager == null) {
                    sManager = new ThreadManager();
                }
            }
        }

        return sManager;
    }

    //子线程执行一个任务
    public void execute(Runnable runnable){
        mExecutor.execute(runnable);
    }

    // 移除一个任务
    public void remove(Runnable runnable){
        mExecutor.remove(runnable);
    }
}
